var settings = require('../settings');
var Db = require('mongodb').Db;
var Server = require('mongodb').Server;
var PoolModule = require('generic-pool');

module.exports = {
    getPool: function () {
        var pool = PoolModule.Pool({
            name: 'mongoPool',
            create: function (callback) {
                var mongodb = initDB();
                mongodb.open(function (err, db) {
                    callback(err, db);
                });
            },
            destroy: function (mongodb) {
                mongodb.close();
            },
            max: 100,
            min: 5,
            idleTimeoutMillis: 30000,
            log: true
        });
        return pool;
    }
};

function initDB() {
    return new Db(settings.db, new Server(settings.host, settings.port), {safe:true, poolSize:5});
}
// 启动数据库的命令
// mongod --dbpath ../blog/